<?php

namespace app\site\controller\site;


use app\common\exception\MiException;
use app\site\controller\AController;
use app\site\controller\traits\CurdTrait;
use app\site\service\site\ArticleService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Request;

class ArticleController extends AController
{
    /**
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public function index(Request $request): \think\response\Json
    {
        $filter = $request->param('where');
        $where = [];
        if ($key = $filter['key']??''){
            $where[] = ['title', 'like', '%'.$key.'%'];
        }
        if ($cid = $filter['cate_id']??0){
            $where[] = ['cate_id', '=', $cid];
        }
        return $this->reqSer->setService(ArticleService::instance()->setWhere($where))->setPaginate()->getResponse();

    }

    /**
     * @throws MiException
     */
    public function add(){
        return $this->reqSer->setService(ArticleService::instance())->add()->getResponse();
    }

    /**
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws MiException
     */
    public function edit(\app\Request $request){
        if($request->isPost()){
            return $this->reqSer->setService(ArticleService::instance())->edit()->getResponse();
        }else{
            return $this->reqSer->setService(ArticleService::instance())->setFind()->getResponse();
        }
    }

    /**
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws MiException
     */
    public function del(){
        return $this->reqSer->setService(ArticleService::instance())->delete()->getResponse();
    }

}
